package com.dlc.shop.systemtest.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.dlc.shop.common.response.ServerResponseEntity;
import com.dlc.shop.common.util.Json;
import com.dlc.shop.systemtest.constants.Constant;

import java.util.List;
import java.util.Objects;

/**
 * @author lanhai
 */
public class HttpUtil {


    public static void get(String url) {
        HttpResponse response = doGet(url);
        CheckUtil.check(response);
    }


    public static void get(String url, UrlEnum urlEnum) {
        HttpResponse response = doGet(url, urlEnum);
        CheckUtil.check(response);
    }

    public static <T> T get(String url, Class<T> clazz) {
        HttpResponse response = doGet(url);
        return CheckUtil.checkAndGetObject(response,clazz);
    }

    public static <T> T get(String url, Class<T> clazz, UrlEnum urlEnum) {
        HttpResponse response = doGet(url, urlEnum);
        return CheckUtil.checkAndGetObject(response,clazz);
    }

    public static <T> T get(String url, Class<T> clazz, Boolean useMainShop) {
        HttpResponse response;
        if(useMainShop) {
            response = doGet(url);
        }else{
            HttpRequest postRequest = HttpRequest.get(url);
            if (url.startsWith(Constant.API_URL)) {
                postRequest.header("authorization", Constant.API_TOKEN);
            }
            if (url.startsWith(Constant.SHOP_URL)) {
                postRequest.header("authorization", Constant.OTHER_SHOP_TOKEN);
            }
            if (url.startsWith(Constant.PLATFORM_URL)) {
                postRequest.header("authorization", Constant.PLATFORM_TOKEN);
            }
            response = postRequest.execute();
        }

        return CheckUtil.checkAndGetObject(response,clazz);
    }

    public static <T> List<T> getResultArray(String url, Class<T[]> clazz) {
        HttpResponse response = doGet(url);
        return CheckUtil.checkAndGetArray(response,clazz);
    }

    public static <T> String gets(String url) {
        HttpResponse response = doGet(url);
        return CheckUtil.checkAndGetObjects(response);
    }


    public static void post(String url, Object obj) {
        HttpResponse response = doPost(url, obj);
        CheckUtil.check(response);
    }


    public static void post(String url, Object obj, UrlEnum urlEnum) {
        HttpResponse response = doPost(url, obj, urlEnum);
        CheckUtil.check(response);
    }
    public static <T> T post(String url, Object obj,Class<T> clazz) {
        HttpResponse response = doPost(url, obj);
        return CheckUtil.checkAndGetObject(response,clazz);
    }
    public static <T> T post(String url, Object obj,Class<T> clazz, UrlEnum urlEnum) {
        HttpResponse response = doPost(url, obj, urlEnum);
        return CheckUtil.checkAndGetObject(response,clazz);
    }
    public static <T> T post(String url, Object obj, Class<T> clazz,Boolean useMainShop) {
        HttpResponse response;
        if(useMainShop) {
            response = doPost(url, obj);
        }else{
            HttpRequest postRequest = HttpRequest.post(url);
            if (url.startsWith(Constant.API_URL)) {
                postRequest.header("authorization", Constant.API_TOKEN);
            }
            if (url.startsWith(Constant.SHOP_URL)) {
                postRequest.header("authorization", Constant.OTHER_SHOP_TOKEN);
            }
            if (url.startsWith(Constant.PLATFORM_URL)) {
                postRequest.header("authorization", Constant.PLATFORM_TOKEN);
            }
            response = postRequest
                    .body(Json.toJsonString(obj))
                    .execute();
        }
        return CheckUtil.checkAndGetObject(response,clazz);
    }


    public static <T> List<T> postResultArray(String url, Object obj, Class<T[]> clazz) {
        HttpResponse response = doPost(url, obj);
        return CheckUtil.checkAndGetArray(response,clazz);
    }


    public static void put(String url, Object obj) {
        HttpResponse response = doPut(url, obj);
        CheckUtil.check(response);
    }


    public static void put(String url, Object obj, UrlEnum urlEnum) {
        HttpResponse response = doPut(url, obj, urlEnum);
        CheckUtil.check(response);
    }
    public static void put(String url) {
        HttpResponse response = doPut(url,null);
        CheckUtil.check(response);
    }
    public static void put(String url, UrlEnum urlEnum) {
        HttpResponse response = doPut(url,null, urlEnum);
        CheckUtil.check(response);
    }
    public static <T> ServerResponseEntity<T> puts(String url, Object obj) {
        HttpResponse response = doPut(url, obj);
        String body = response.body();
        TypeReference<ServerResponseEntity<T>> typeReference = new TypeReference<ServerResponseEntity<T>>(ServerResponseEntity.class) {};
        ServerResponseEntity<T> serverResponseEntity = JSON.parseObject(body, typeReference);
        return serverResponseEntity;
    }

    public static void put(String url, Object obj,Boolean useMainShop) {
        HttpResponse response;
        if (useMainShop){
            response = doPut(url, obj);
        }else {
            HttpRequest putRequest = HttpRequest.put(url);
            if (url.startsWith(Constant.API_URL)) {
                putRequest.header("authorization", Constant.API_TOKEN);
            }
            if (url.startsWith(Constant.SHOP_URL)) {
                putRequest.header("authorization", Constant.OTHER_SHOP_TOKEN);
            }
            if (url.startsWith(Constant.PLATFORM_URL)) {
                putRequest.header("authorization", Constant.PLATFORM_TOKEN);
            }
            response = putRequest
                    .body(Json.toJsonString(obj))
                    .execute();
        }
        CheckUtil.check(response);
    }

    public static <T> List<T> putResultArray(String url, Object obj, Class<T[]> clazz) {
        HttpResponse response = doPut(url, obj);
        return CheckUtil.checkAndGetArray(response,clazz);
    }


    public static void delete(String url) {
        HttpResponse response = doDelete(url);
        CheckUtil.check(response);
    }

    public static void delete(String url, Object obj) {
        HttpResponse response = doDelete(url, obj);
        CheckUtil.check(response);
    }

    public static <T> T delete(String url, Class<T> clazz) {
        HttpResponse response = doDelete(url);
        return CheckUtil.checkAndGetObject(response,clazz);
    }

    public static <T> List<T> deleteResultArray(String url, Class<T[]> clazz) {
        HttpResponse response = doDelete(url);
        return CheckUtil.checkAndGetArray(response,clazz);
    }



    private static HttpResponse doGet(String url) {
        return doGet(url, null);
    }



    private static HttpResponse doGet(String url, UrlEnum urlEnum) {
        HttpRequest postRequest = HttpRequest.get(url);
        setHeader(postRequest, urlEnum);
        return postRequest.execute();
    }

    private static HttpResponse doPost(String url, Object obj) {
        return doPost(url, obj, null);
    }

    private static HttpResponse doPost(String url, Object obj, UrlEnum urlEnum) {
        HttpRequest postRequest = HttpRequest.post(url);
        setHeader(postRequest, urlEnum);
        // 如果是String类型，直接发送， 通过json转换成String发送会多出双引号
        if (obj instanceof String) {
            return postRequest.body(obj.toString()).execute();
        }
        return postRequest
                .body(JSON.toJSONString(obj))
                .execute();
    }

    private static HttpResponse doPut(String url, Object obj) {
        return doPut(url, obj, null);
    }

    /**
     * 需要权限校验的put请求
     * @param url
     * @param obj
     * @param urlEnum com.dlc.shop.systemtest.util.UrlEnum
     * @return
     */
    private static HttpResponse doPut(String url, Object obj, UrlEnum urlEnum) {
        HttpRequest postRequest = HttpRequest.put(url);
        setHeader(postRequest, urlEnum);
        // 如果是String类型，直接发送， 通过json转换成String发送会多出双引号
        if (obj instanceof String) {
            return postRequest.body(obj.toString()).execute();
        }
        return postRequest
                .body(JSON.toJSONString(obj))
                .execute();
    }

    private static HttpResponse doDelete(String url) {
        return doDelete(url, null);
    }

    private static HttpResponse doDelete(String url, UrlEnum urlEnum) {
        HttpRequest postRequest = HttpRequest.delete(url);
        setHeader(postRequest, urlEnum);
        return postRequest.execute();
    }

    private static HttpResponse doDelete(String url, Object obj) {
        return doDelete(url, obj, null);
    }

    private static HttpResponse doDelete(String url, Object obj, UrlEnum urlEnum) {
        HttpRequest postRequest = HttpRequest.delete(url);
        setHeader(postRequest, urlEnum);
        return postRequest
                .body(Json.toJsonString(obj))
                .contentType("application/json")
                .execute();
    }

    private static HttpRequest setHeader(HttpRequest httpRequest) {
        return setHeader(httpRequest, null);
    }

    private static HttpRequest setHeader(HttpRequest httpRequest, UrlEnum urlEnum) {
        httpRequest.header("content-type", "application/json");
        // 自定义token
        if (Objects.nonNull(urlEnum)) {
            httpRequest.header("authorization", urlEnum.token());
            return httpRequest;
        }
        String url = httpRequest.getUrl();
        if (url.startsWith(Constant.API_URL)) {
            httpRequest.header("authorization", Constant.API_TOKEN);
        }
        if (url.startsWith(Constant.SHOP_URL)) {
            httpRequest.header("authorization", Constant.SHOP_TOKEN);
        }
        if (url.startsWith(Constant.PLATFORM_URL)) {
            httpRequest.header("authorization", Constant.PLATFORM_TOKEN);
        }
        return httpRequest;
    }
}
